//
//  LocationViewController.m
//  HTP
//
//  Created by 陈钱钿 on 15/11/23.
//  Copyright © 2015年 nit. All rights reserved.
//

#import "LocationViewController.h"
#import <BaiduMapAPI_Search/BMKSearchComponent.h>
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <CoreLocation/CoreLocation.h>
@interface LocationViewController () <CLLocationManagerDelegate, BMKGeoCodeSearchDelegate,UITableViewDataSource,UITableViewDelegate,BMKPoiSearchDelegate,UISearchBarDelegate,BMKSuggestionSearchDelegate, UIAlertViewDelegate>
@property (nonatomic, retain) UIView *BtnView;
@property (nonatomic, retain) BMKMapView *mapView;
@property (nonatomic, retain) BMKPoiSearch *searchSchool;
@property (nonatomic, retain) BMKPoiSearch *searchHouse;
@property (nonatomic, retain) BMKPoiSearch *searchOffice;
@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, retain) BMKGeoCodeSearch *search;
@property (nonatomic, retain) UITableView *allTable;
@property (nonatomic, retain) UITableView *officeTable;
@property (nonatomic, retain) UITableView *houseTable;
@property (nonatomic, retain) UITableView *schoolTable;
@property (nonatomic, retain) NSMutableArray *allItems;
@property (nonatomic, retain) NSMutableArray *allDetailItems;
@property (nonatomic, retain) NSMutableArray *officeItems;
@property (nonatomic, retain) NSMutableArray *officeDetails;
@property (nonatomic, retain) NSMutableArray *houseItems;
@property (nonatomic, retain) NSMutableArray *houseDetails;
@property (nonatomic, retain) NSMutableArray *schoolItems;
@property (nonatomic, retain) NSMutableArray *schoolDetails;
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) UIButton *allBtn;
@property (nonatomic, retain) UIButton *officeBtn;
@property (nonatomic, retain) UIButton *houseBtn;
@property (nonatomic, retain) UIButton *schoolBtn;
@property (nonatomic, retain) UISearchBar *searchBar;
@property (nonatomic, retain) UIButton *btnAccessoryView;
@property (nonatomic, retain) BMKSuggestionSearch *suggestSearch;
@property (nonatomic, retain) UITableView *searchTable;
@property (nonatomic, retain) NSMutableArray *searchItem;
@property (nonatomic, retain) NSMutableArray *searchDetails;
@end

@implementation LocationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self showSvprogress:@""];
    [self coreLocation];
    [self addCustomBackBase];
    [self initSearchBar];
    _suggestSearch = [[BMKSuggestionSearch alloc] init];
    _suggestSearch.delegate = self;
    
    
}
//TODO:初始化MapView 在获得经纬度后加载
- (void)initMapView {
    _mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeihgt/3+50)];
    _mapView.centerCoordinate = _coordinate;
    _mapView.zoomLevel = 16;
    [self.view addSubview:_mapView];
    //标识
    BMKPointAnnotation *annotation = [[BMKPointAnnotation alloc] init];
    annotation.coordinate = _coordinate;
    annotation.title = @"你在这里";
    [_mapView addAnnotation:annotation];
    [self initBtnView];
    [self.view addSubview:self.allTable];
    [_allBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
}
//TODO:初始化searchBar
- (void)initSearchBar {
    _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(50, 0, ScreenWidth-50, 44)];
    [self addCustomTitleView:_searchBar];
    [_searchBar setPlaceholder:@"小区/写字楼/学校等"];
    _searchBar.delegate = self;
    
}
- (void)ClickControlAction {
//    DLog(@"hanleTaps");
    [self controlAccessoryView:0.0f];
}
- (void)controlAccessoryView:(float)alphaValue {
    [UIView animateWithDuration:0.2 animations:^{
        [_btnAccessoryView setAlpha:alphaValue];
        if (alphaValue <= 0) {
            [_searchBar resignFirstResponder];
            [_searchBar setShowsCancelButton:NO animated:YES];
        }
    }];
}

//TODO:searchBar delegate
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    [_searchBar setShowsCancelButton:YES animated:YES];
    //遮盖层
    _btnAccessoryView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeihgt)];
    [_btnAccessoryView setBackgroundColor:[UIColor blackColor]];
    [_btnAccessoryView setAlpha:0.3f];
    [_btnAccessoryView addTarget:self action:@selector(ClickControlAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_btnAccessoryView];
    [_btnAccessoryView bringSubviewToFront:self.view];
    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0){
        for (id cc in [_searchBar.subviews[0] subviews]) {
            if([cc isKindOfClass:[UIButton class]]){
                UIButton *btn = (UIButton *)cc;
                [btn setTitle:@"取消"  forState:UIControlStateNormal];
            }
        }
    }else {
        for (id cc in [_searchBar subviews]) {
            if([cc isKindOfClass:[UIButton class]]){
                UIButton *btn = (UIButton *)cc;
                [btn setTitle:@"取消"  forState:UIControlStateNormal];
            }
        }
    }
}
/*
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
    return YES;
}*/
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
//    DLog(@"searchBarTextDidEndEditing");
    [self.searchTable removeFromSuperview];
    _searchTable = nil;
}

//TODO:键盘中，搜索按钮被按下时执行的方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {

}
//TODO:searchBar中的取消按钮按下时 执行的方法
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
//    DLog(@"searchBarCancelButtonClicked");
    [_searchBar resignFirstResponder];
    [_searchBar setShowsCancelButton:NO animated:YES];
    [self controlAccessoryView:0]; //隐藏遮盖层
    [self.searchTable removeFromSuperview];
    _searchTable = nil;
}
//TODO:实时搜索
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    if (_searchBar.text.length > 0) {
//        [self.view addSubview:self.searchTable];
//        [self.searchTable bringSubviewToFront:self.view];
        BMKSuggestionSearchOption *option = [[BMKSuggestionSearchOption alloc] init];
        option.keyword = _searchBar.text;
        option.cityname = [USERCONTENT getAreaMessgae];
        BOOL flag = [_suggestSearch suggestionSearch:option];
        if(flag) {DLog(@"建议检索发送成功");
            [self.searchTable removeFromSuperview];}
        else {DLog(@"建议检索发送失败")
            [self showSVProgressFailed:@"抱歉，未找到结果"];
        }
    }else {
        [self.searchItem removeAllObjects];
        [self.searchDetails removeAllObjects];
        [self.searchTable removeFromSuperview];
        _searchTable = nil;
    }
}
//TODO:delegate回调建议检索结果
- (void)onGetSuggestionResult:(BMKSuggestionSearch *)searcher result:(BMKSuggestionResult *)result errorCode:(BMKSearchErrorCode)error {
    if(self.searchDetails.count > 0){
        [self.searchDetails removeAllObjects];
        [self.searchItem removeAllObjects];
        _searchTable = nil;
    }
    if (error == BMK_SEARCH_NO_ERROR) {
        for (int i=0; i<result.districtList.count; i++) {
            [self.searchDetails addObject:result.districtList[i]];
        }
        for (int i=0; i<result.keyList.count; i++) {
            [self.searchItem addObject:result.keyList[i]];
        }
        [self.view addSubview:self.searchTable];
        [self.searchTable reloadData];
    }else{
        DLog(@"建议检索出错");
        [self showSVProgressFailed:@"抱歉，未找到结果"];
    }
}
//TODO:视图消失时取消代理
- (void)viewWillDisappear:(BOOL)animated {
    _search.delegate = nil;
    _searchSchool.delegate = nil;
    _searchHouse.delegate = nil;
    _searchOffice.delegate = nil;
    _suggestSearch.delegate = nil;
}
//TODO:初始化按钮视图
- (void)initBtnView{
    _BtnView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_mapView.frame), ScreenWidth, 30)];
    [self.view addSubview:_BtnView];
    _allBtn = [self ButtonWithFrame:CGRectMake(0, 0, ScreenWidth/4, 30) title:@"全部"];
    [_allBtn addTarget:self action:@selector(allBtnClick) forControlEvents:UIControlEventTouchUpInside];
    _officeBtn = [self ButtonWithFrame:CGRectMake(1*ScreenWidth/4, 0, ScreenWidth/4, 30) title:@"写字楼"];
    [_officeBtn addTarget:self action:@selector(officeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    _houseBtn = [self ButtonWithFrame:CGRectMake(2*ScreenWidth/4, 0, ScreenWidth/4, 30) title:@"小区"];
    [_houseBtn addTarget:self action:@selector(houseBtnClick) forControlEvents:UIControlEventTouchUpInside];
    _schoolBtn = [self ButtonWithFrame:CGRectMake(3*ScreenWidth/4, 0, ScreenWidth/4, 30) title:@"高校"];
    [_schoolBtn addTarget:self action:@selector(schoolBtnClick) forControlEvents:UIControlEventTouchUpInside];
}
//TODO:初始化按钮的封装
- (UIButton *)ButtonWithFrame:(CGRect)frame title:(NSString *)title{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = frame;
    [btn setTitle:title forState:UIControlStateNormal];
    [_BtnView addSubview:btn];
    return btn;
}
//TODO:定位控制器的初始化 -- wangxin
- (void)coreLocation {
    _search = [[BMKGeoCodeSearch alloc] init];
    _search.delegate = self;
    _locationManager = [[CLLocationManager alloc] init];
    [_locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
    _locationManager.delegate = self;
    if([CLLocationManager locationServicesEnabled] && ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined)){
        DLog(@"已开启定位");
        self.locationManager.distanceFilter = 1000;
        //设置定位的精准度 经度越高 越耗电 这里设置为精度最高的
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
        [self.locationManager startUpdatingLocation];
    }else {
        DLog(@"nnno");
        //提醒用户打开定位服务
        [_locationManager requestWhenInUseAuthorization];
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"定位服务未开启" message:@"请在系统设置中开启定位服务(定位服务->青苗家教->使用应用期间)" delegate:nil cancelButtonTitle:@"定位服务" otherButtonTitles:@"取消" ,nil];
        alter.delegate = self;
        [alter show];
        [self dismissSVProgress];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        NSURL *url = [NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"];
        if ([[UIApplication sharedApplication] canOpenURL:url])
        {
            [[UIApplication sharedApplication] openURL:url];
        }
    }
}

/** location -- wangxin
 *
 *  CLLocationManagerDelegate
 **/
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    switch (status) {
        case kCLAuthorizationStatusNotDetermined:
            if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
                [self.locationManager requestWhenInUseAuthorization];
            }
            break;
        default:
            break;
    }
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    [self showSVProgressFailed:@"网络错误"];
    NSLog(@"%@",error);
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    CLLocation *newLocation = locations[0];
    //得到经纬度
    CLLocationCoordinate2D oldCoordinate = newLocation.coordinate;
    _coordinate = newLocation.coordinate;
    DLog(@"经度：%f,纬度：%f",oldCoordinate.longitude,oldCoordinate.latitude);
    [manager stopUpdatingLocation];
    //在这里初始化MapView
    [self initMapView];
    [self dismissSVProgress];
    //发送地理坐标反编码
    BMKReverseGeoCodeOption *reversGeoCodeOption = [[BMKReverseGeoCodeOption alloc] init];
    reversGeoCodeOption.reverseGeoPoint = oldCoordinate;
    BOOL flag = [_search reverseGeoCode:reversGeoCodeOption];
    if (flag) {
        NSLog(@"发送成功");
    }else{
        NSLog(@"发送失败");
        [self showSVProgressFailed:@"网络错误"];
    }
}
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error {
    if (error == BMK_SEARCH_NO_ERROR) {
        for (BMKPoiInfo *info in result.poiList) {
            [self.allItems addObject:info.name];
            [self.allDetailItems addObject:info.address];
        }
        [self dismissSVProgress];
        [self.allTable reloadData];
    }else{
        [self showSVProgressFailed:@"网络错误"];
    }
}
//TODO:学校检索
- (void)searchSchoolPOI{
    _searchSchool = [[BMKPoiSearch alloc] init];
    _searchSchool.delegate = self;
    BMKNearbySearchOption *option = [self getOption:@"学校"];
    BOOL flag = [_searchSchool poiSearchNearBy:option];
    if(flag){DLog(@"周边检索发送成功");}
    else{
        DLog(@"周边检索发送失败");
        [self showSVProgressFailed:@"网络错误"];
    }
}
//TODO:写字楼检索
- (void)searchOfficeBuildingPOI {
    _searchOffice = [[BMKPoiSearch alloc] init];
    _searchOffice.delegate = self;
    BMKNearbySearchOption *option = [self getOption:@"写字楼"];
    BOOL flag = [_searchOffice poiSearchNearBy:option];
    if(flag){DLog(@"周边检索发送成功");}
    else{DLog(@"周边检索发送失败");
        [self showSVProgressFailed:@"网络错误"];
    }
}
//TODO:小区检索
- (void)searchHouseBuildingPOI {
    _searchHouse = [[BMKPoiSearch alloc] init];
    _searchHouse.delegate = self;
    BMKNearbySearchOption *option = [self getOption:@"小区"];
    BOOL flag = [_searchHouse poiSearchNearBy:option ];
    if(flag){DLog(@"周边检索发送成功");}
    else{DLog(@"周边检索发送失败");
        [self showSVProgressFailed:@"网络错误"];
    }
}
//TODO:初始化周边云检索参数信息类
- (BMKNearbySearchOption *)getOption:(NSString *)keyword {
    BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc]init];
    option.pageIndex = 0;
    option.pageCapacity = 15;
    option.location = _coordinate;
    option.keyword = keyword;
    option.radius = 1000;
    return option;
}
//TODO:BMKPoiSearch代理 返回检索数据
- (void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult *)poiResult errorCode:(BMKSearchErrorCode)errorCode {
    if(searcher == _searchSchool){
        if(errorCode == BMK_SEARCH_NO_ERROR) {
            for (int i=0; i<poiResult.poiInfoList.count; i++) {
                [self.schoolItems addObject:[poiResult.poiInfoList[i] name]];
                [self.schoolDetails addObject:[poiResult.poiInfoList[i] address]];
            }
            [self.schoolTable reloadData];
            [self dismissSVProgress];
        }else [self showSVProgressFailed:@"网络错误"];
    }
    else if (searcher == _searchOffice) {
        if(errorCode == BMK_SEARCH_NO_ERROR) {
            for (int i=0; i<poiResult.poiInfoList.count; i++) {
                [self.officeItems addObject:[poiResult.poiInfoList[i] name]];
                [self.officeDetails addObject:[poiResult.poiInfoList[i] address]];
            }
            [self.officeTable reloadData];
            [self dismissSVProgress];
        }else [self showSVProgressFailed:@"网络错误"];
    }else if(searcher == _searchHouse) {
        if(errorCode == BMK_SEARCH_NO_ERROR) {
            for (int i=0; i<poiResult.poiInfoList.count; i++) {
//                NSLog(@"%@",[poiResult.poiInfoList[i] address]);
                [self.houseItems addObject:[poiResult.poiInfoList[i] name]];
                [self.houseDetails addObject:[poiResult.poiInfoList[i] address]];
            }
            [self.houseTable reloadData];
            [self dismissSVProgress];
        }else [self showSVProgressFailed:@"网络错误"];
    }
}
//TODO:tableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == _allTable) {
        return self.allItems.count;
    }else if(tableView == _officeTable){
        return self.officeItems.count;
    }else if(tableView == _houseTable){
        return self.houseItems.count;
    }else if (tableView == _schoolTable){
        return self.schoolItems.count;
    }else if(tableView == _searchTable){
//        DLog(@"%lu",(unsigned long)self.searchItem.count);
        DLog(@"numbel-----%lu",(unsigned long)self.searchItem.count);
        return self.searchItem.count;
    }
    else
        return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        cell.imageView.image = [UIImage imageNamed:@"local"];
        if(tableView == _allTable){
            cell.textLabel.text = self.allItems[indexPath.row];
            cell.detailTextLabel.text = self.allDetailItems[indexPath.row];
        }else if (tableView == _officeTable){
            cell.textLabel.text = self.officeItems[indexPath.row];
            cell.detailTextLabel.text = self.officeDetails[indexPath.row];
        }else if (tableView == _schoolTable){
            cell.textLabel.text = self.schoolItems[indexPath.row];
            cell.detailTextLabel.text = self.schoolDetails[indexPath.row];
        }else if (tableView == _houseTable){
            cell.textLabel.text = self.houseItems[indexPath.row];
            cell.detailTextLabel.text = self.houseDetails[indexPath.row];
        }else if (tableView == _searchTable){
            cell.imageView.image = nil;
            cell.textLabel.text = self.searchItem[indexPath.row];
            cell.detailTextLabel.text = self.searchDetails[indexPath.row];
        }
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    UITableViewCell *cell = [tableView]
    NSIndexPath *path = [tableView indexPathForSelectedRow];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if([self.delegate respondsToSelector:@selector(passLocation:andDetail:)]){
        [self.delegate passLocation:cell.textLabel.text andDetail:cell.detailTextLabel.text];
    }
    [self.navigationController popViewControllerAnimated:YES];
}
//TODO:按钮的点击事件
- (void)allBtnClick {
    [_houseBtn setTitleColor:RGBCOLOR(13.0, 95.0, 255.0, 1) forState:UIControlStateNormal];
    [_schoolBtn setTitleColor:RGBCOLOR(13.0, 95.0, 255.0, 1) forState:UIControlStateNormal];
    [_officeBtn setTitleColor:RGBCOLOR(13.0, 95.0, 255.0, 1) forState:UIControlStateNormal];
    [_allBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
//    [self showSvprogress:@""];
    [self.view addSubview:self.allTable];
//    [self dismissSVProgress];
}
- (void)officeBtnClick {
    [_allBtn setTitleColor:RGBCOLOR(13.0, 95.0, 255.0, 1)forState:UIControlStateNormal];
    [_houseBtn setTitleColor:RGBCOLOR(13.0, 95.0, 255.0, 1) forState:UIControlStateNormal];
    [_schoolBtn setTitleColor:RGBCOLOR(13.0, 95.0, 255.0, 1) forState:UIControlStateNormal];
    [_officeBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [self showSvprogress:@""];
    [self searchOfficeBuildingPOI];
    [self.view addSubview:self.officeTable];
    [self.allTable removeFromSuperview];
//    [self dismissSVProgress];
}
- (void)houseBtnClick {
    [_allBtn setTitleColor:RGBCOLOR(13.0, 95.0, 255.0, 1) forState:UIControlStateNormal];
    [_houseBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [_schoolBtn setTitleColor:RGBCOLOR(13.0, 95.0, 255.0, 1) forState:UIControlStateNormal];
    [_officeBtn setTitleColor:RGBCOLOR(13.0, 95.0, 255.0, 1) forState:UIControlStateNormal];
    [self showSvprogress:@""];
    [self searchHouseBuildingPOI];
    [self.view addSubview:self.houseTable];
    [self.officeTable removeFromSuperview];
//    [self dismissSVProgress];
}
- (void)schoolBtnClick {
    [_allBtn setTitleColor:RGBCOLOR(13.0, 95.0, 255.0, 1) forState:UIControlStateNormal];
    [_houseBtn setTitleColor:RGBCOLOR(13.0, 95.0, 255.0, 1) forState:UIControlStateNormal];
    [_schoolBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [_officeBtn setTitleColor:RGBCOLOR(13.0, 95.0, 255.0, 1) forState:UIControlStateNormal];
    [self showSvprogress:@""];
    [self searchSchoolPOI];
    [self.view addSubview:self.schoolTable];
    [self.houseTable removeFromSuperview];
//    [self dismissSVProgress];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//TODO:懒加载
- (NSMutableArray *)allItems {
    if(!_allItems){
        _allItems = [NSMutableArray array];
    }
    return _allItems;
}
- (NSMutableArray *)allDetailItems {
    if(!_allDetailItems){
        _allDetailItems = [NSMutableArray array];
    }
    return _allDetailItems;
}
- (NSMutableArray *)officeItems {
    if(!_officeItems){
        _officeItems = [NSMutableArray array];
    }
    return _officeItems;
}
- (NSMutableArray *)officeDetails {
    if(!_officeDetails){
        _officeDetails = [NSMutableArray array];
    }
    return _officeDetails;
}
- (NSMutableArray *)houseItems {
    if(!_houseItems){
        _houseItems = [NSMutableArray array];
    }
    return _houseItems;
}
- (NSMutableArray *)houseDetails {
    if(!_houseDetails){
        _houseDetails = [NSMutableArray array];
    }
    return _houseDetails;
}
- (NSMutableArray *)schoolItems {
    if(!_schoolItems){
        _schoolItems = [NSMutableArray array];
    }
    return _schoolItems;
}
- (NSMutableArray *)schoolDetails {
    if(!_schoolDetails){
        _schoolDetails = [NSMutableArray array];
    }
    return _schoolDetails;
}
- (NSMutableArray *)searchItem {
    if (!_searchItem) {
        _searchItem = [NSMutableArray array];
    }
    return _searchItem;
}
- (NSMutableArray *)searchDetails {
    if(!_searchDetails){
        _searchDetails = [NSMutableArray array];
    }
    return _searchDetails;
}

- (UITableView *)allTable {
    if(!_allTable){
        _allTable = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_BtnView.frame), ScreenWidth, ScreenHeihgt - _mapView.frame.size.height - _BtnView.frame.size.height - 64)];
        _allTable.delegate = self;
        _allTable.dataSource = self;
    }
    return _allTable;
}
- (UITableView *)officeTable {
    if(!_officeTable){
        _officeTable = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_BtnView.frame), ScreenWidth, ScreenHeihgt - _mapView.frame.size.height - _BtnView.frame.size.height - 64)];
        _officeTable.delegate = self;
        _officeTable.dataSource = self;
    }
    return _officeTable;
}
- (UITableView *)houseTable {
    if(!_houseTable){
        _houseTable = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_BtnView.frame), ScreenWidth, ScreenHeihgt - _mapView.frame.size.height - _BtnView.frame.size.height - 64)];
        _houseTable.delegate = self;
        _houseTable.dataSource = self;
    }
    return _houseTable;
}
- (UITableView *)schoolTable {
    if(!_schoolTable){
        _schoolTable = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_BtnView.frame), ScreenWidth, ScreenHeihgt - _mapView.frame.size.height - _BtnView.frame.size.height - 64)];
        _schoolTable.delegate = self;
        _schoolTable.dataSource = self;
    }
    return _schoolTable;
}
- (UITableView *)searchTable {
    if(!_searchTable){
        _searchTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeihgt-64)];
        _searchTable.delegate = self;
        _searchTable.dataSource = self;
//        _searchTable.rowHeight = 40;
    }
    return _searchTable;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
